package com.sgzn.sgpay.service.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.sgzn.sgpay.model.request.SgsShowIncomeRecordRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.vo.ShowIncomeRecordVo;
import com.sgzn.sgpay.persistence.dao.TShowIncomeRecordMapper;
import com.sgzn.sgpay.persistence.domain.TShowIncomeRecord;
import com.sgzn.sgpay.persistence.domain.TShowIncomeRecordExample;
import com.sgzn.sgpay.service.SgsShowIncomeRecordService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 首页收益统计服务接口实现类
 * @author huangsenguang
 * @version Id: SgsShowIncomeRecordServiceImpl.java, v 0.1 2018/6/13 11:55 huangsenguang Exp $$
 */
@Service
@Slf4j
public class SgsShowIncomeRecordServiceImpl implements SgsShowIncomeRecordService {
    @Autowired
    private TShowIncomeRecordMapper tShowIncomeRecordMapper;
    @Override
    public SgsResultResponse<ShowIncomeRecordVo> query(SgsShowIncomeRecordRequest request) {
        TShowIncomeRecordExample tShowIncomeRecordExample = new TShowIncomeRecordExample();
        tShowIncomeRecordExample.createCriteria().andMerchantIdEqualTo(request.getMchId());
        List<TShowIncomeRecord> tShowIncomeRecords = tShowIncomeRecordMapper.selectByExample(tShowIncomeRecordExample);
        ShowIncomeRecordVo result = new ShowIncomeRecordVo();;
        if (!CollectionUtils.isEmpty(tShowIncomeRecords)) {
            assembleResponse(tShowIncomeRecords, result);
        }
        return SgsResponseUtil.INSTANCE.success(result);
    }

    private void assembleResponse(List<TShowIncomeRecord> tShowIncomeRecords, ShowIncomeRecordVo result) {
        TShowIncomeRecord tShowIncomeRecord = tShowIncomeRecords.get(0);
        result.setMerchantId(tShowIncomeRecord.getMerchantId());

        boolean flag = judge(tShowIncomeRecord.getUpdatedAt());

        //今日收益
        if (flag) {
            result.setTodayOnlineAmount(tShowIncomeRecord.getTodayOnlineAmount());
            result.setTodayConsumePresentAmount(tShowIncomeRecord.getTodayConsumePresentAmount());
            result.setTodayCashIncome(tShowIncomeRecord.getTodayCashIncome());
            result.setAdIncome(tShowIncomeRecord.getAdIncome());
            result.setTodaySlitCoinQuantity(tShowIncomeRecord.getTodaySlitCoinQuantity());
        } else {
            result.setTodayOnlineAmount(new BigDecimal(0));
            result.setTodayConsumePresentAmount(0);
            result.setTodayCashIncome(new BigDecimal(0));
            result.setAdIncome(new BigDecimal(0));
            result.setTodaySlitCoinQuantity(0);
        }

        //累计收益
        result.setOnlineAmount(tShowIncomeRecord.getOnlineAmount());
        result.setConsumePresentAmount(tShowIncomeRecord.getConsumePresentAmount());
        result.setCashIncome(tShowIncomeRecord.getCashIncome());
        result.setAdIncome(tShowIncomeRecord.getAdIncome());
        result.setSlitCoinQuantity(tShowIncomeRecord.getSlitCoinQuantity());
    }

    /**
     * 判断修改的时间是否是当天：true 当天， false 不是当天
     * @param updatedAt
     * @return
     */
    private boolean judge(Date updatedAt) {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String update = format.format(updatedAt);
        String now = format.format(date);
        String[] updates = update.split("-");
        String[] nows = now.split("-");

        for (int index = 0; index <updates.length; index++) {
            if (Integer.parseInt(updates[index]) != Integer.parseInt(nows[index])) {
                return false;
            }
        }
        return true;
    }
}

